<?php 
class Bitly
{
	private $Username;
	private $API_key;

	function __construct()
	{
		$this->Username = REST_BITLY_API_USERNAME;
		$this->API_key = REST_BITLY_API_KEY;
	}

	public function Shorten_url($url)
	{
		$api_url = "https://api-ssl.bitly.com/v3/shorten?access_token=".$this->API_key."&longUrl=".urlencode($url)."&format=json";

		$handle = curl_init();
		curl_setopt($handle, CURLOPT_URL, $api_url);
		curl_setopt($handle, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($handle, CURLOPT_HEADER, 0);
		curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

		$response = curl_exec($handle);// get json response and convert into array
		
		$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
		curl_close ($handle);

		$response_array = json_decode($response, true);
		//get url from the array
		//print_r($response_array);
		return array(
			'code' => $response_array['status_code'],
			'url' => $response_array['data']['url']
		);
	}
}
?>